7

मेरा एप्लिकेशन SeekableByteChannel इंटरफेस के अहसास के माध्यम से एसडी कार्ड यादृच्छिक रूप से पर फ़ाइल में बाइट पढ़ने/लिखने के लिए जावा क्लास RandomAccessFile का उपयोग करता है। अब मुझे एंड्रॉइड 5.0 के लिए नए लॉलीपॉप एपीआई के साथ फिर से लिखना होगा।लॉलीपॉप के लिए प्रस्तुत एपीआई के माध्यम से एसडी-कार्ड पर फ़ाइल में यादृच्छिक पहुंच कैसे प्राप्त करें?

मैं the only way पाया है पढ़ने के लिए:

InputStream inputStream = getContentResolver().openInputStream(uri); 

और लिखें:

ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w"); 
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); 
/से

नई एपीआई में एक फाइल करने के लिए।

मैं कुछ यादृच्छिक स्थिति में चैनल सेट करने और उस स्थिति को पढ़ने/लिखने के बाइट्स में चैनल सेट करने की क्षमता रखना चाहता हूं। क्या यह नए एसडीके 21 में करना संभव है? क्या नया एसडीके चैनलों को प्राप्त करने का तरीका बताता है:

FieInputChannel fieInputChannel = fileInputStream.getChannel(); 
FieOutputChannel fieOutputChannel = fileOutputStream.getChannel(); 

या कोई अन्य दृष्टिकोण?

उत्तर

7

यह एक यादृच्छिक पढ़ने/प्राप्त एसडी एसडीके 21 (लॉलीपॉप) के लिए कार्ड पर एक फ़ाइल के लिए उपयोग लिखने के लिए एक ही रास्ता लगता है इस प्रकार है:

import android.content.Context; 
import android.net.Uri; 
import android.os.ParcelFileDescriptor; 
import com.jetico.bestcrypt.FileManagerApplication; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 

public class SecondaryCardChannel {//By analogy with the java.nio.channels.SeekableByteChannel 
    private FileChannel fileChannel; 
    private ParcelFileDescriptor pfd; 
    private boolean isInputChannel; 
    private long position; 

    public SecondaryCardChannel(Uri treeUri, Context context) { 
     try { 
      pfd = context.getContentResolver().openFileDescriptor(treeUri, "rw"); 
      FileInputStream fis = new FileInputStream(pfd.getFileDescriptor()); 
      fileChannel = fis.getChannel(); 
      isInputChannel = true; 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 

    public int read(ByteBuffer buffer) { 
     if (!isInputChannel) { 
      try { 
       fileChannel.close(); 
       FileInputStream fis = new FileInputStream(pfd.getFileDescriptor()); 
       fileChannel = fis.getChannel(); 
       isInputChannel = true; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     try { 
      fileChannel.position(position); 
      int bytesRead = fileChannel.read(buffer); 
      position = fileChannel.position(); 
      return bytesRead; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return -1; 
     } 
    } 

    public int write(ByteBuffer buffer) { 
     if (isInputChannel) { 
      try { 
       fileChannel.close(); 
       FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor()); 
       fileChannel = fos.getChannel(); 
       isInputChannel = false; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     try { 
      fileChannel.position(position); 
      int bytesWrite = fileChannel.write(buffer); 
      position = fileChannel.position(); 
      return bytesWrite; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return -1; 
     } 
    } 

    public long position() throws IOException { 
     return position; 
    } 

    public SecondaryCardChannel position(long newPosition) throws IOException { 
     position = newPosition; 
     return this; 
    } 

    public long size() throws IOException { 
     return fileChannel.size(); 
    } 

    public SecondaryCardChannel truncate(long size) throws IOException { 
     fileChannel.truncate(size); 
     return this; 
    } 
} 

संपादित 15/03/2017: थोड़ा सा अनुकूलित संस्करण

import android.content.Context; 
import android.net.Uri; 
import android.os.ParcelFileDescriptor; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 

public class SecondaryCardChannel { 
    private ParcelFileDescriptor pfdInput, pfdOutput; 
    private FileInputStream fis; 
    private FileOutputStream fos; 
    private long position; 

    public SecondaryCardChannel(Uri treeUri, Context context) { 
     try { 
      pfdInput = context.getContentResolver().openFileDescriptor(treeUri, "r"); 
      pfdOutput = context.getContentResolver().openFileDescriptor(treeUri, "rw"); 
      fis = new FileInputStream(pfdInput.getFileDescriptor()); 
      fos = new FileOutputStream(pfdOutput.getFileDescriptor()); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 

    public int read(ByteBuffer buffer) { 
     try { 
      FileChannel fch = fis.getChannel(); 
      fch.position(position); 
      int bytesRead = fch.read(buffer); 
      position = fch.position(); 
      return bytesRead; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return -1; 
     } 
    } 

    public int write(ByteBuffer buffer) { 
     try { 
      FileChannel fch = fos.getChannel(); 
      fch.position(position); 
      int bytesWrite = fch.write(buffer); 
      position = fch.position(); 
      return bytesWrite; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return -1; 
     } 
    } 

    public long position() throws IOException { 
     return position; 
    } 

    public SecondaryCardChannel position(long newPosition) throws IOException { 
     position = newPosition; 
     return this; 
    } 

    public long size() throws IOException { 
     return fis.getChannel().size(); 
    } 

    public void force(boolean metadata) throws IOException { 
     fos.getChannel().force(metadata); 
     pfdOutput.getFileDescriptor().sync(); 
    } 

    public long truncate(long size) throws Exception { 
     FileChannel fch = fos.getChannel(); 
     try { 
      fch.truncate(size); 
      return fch.size(); 
     } catch (Exception e){ // Attention! Truncate is broken on removable SD card of Android 5.0 
      e.printStackTrace(); 
      return -1; 
     } 
    } 

    public void close() throws IOException { 
     FileChannel fch = fos.getChannel(); 
     fch.close(); 

     fos.close(); 
     fis.close(); 
     pfdInput.close(); 
     pfdOutput.close(); 
    } 
} 
जैसा दिखता है
संबंधित मुद्दे

 संबंधित मुद्दे